/** This program is free software: you can redistribute it and/or modify
    it under the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation, either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program. If not, see < http://www.gnu.org/licenses/>.
*/

package dz.borconlib.logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import dz.borconlib.events.BOAbstractEvent;
import dz.borconlib.events.DeadEvent;
import dz.borconlib.events.ExitLevelEvent;
import dz.borconlib.events.InitGameEvent;
import dz.borconlib.events.JoinEvent;
import dz.borconlib.events.KillEvent;
import dz.borconlib.events.QuitEvent;
import dz.borconlib.events.SayEvent;
import dz.borconlib.events.ShutdownGame;
import dz.borconlib.events.WeaponEvent;
import dz.borconlib.utils.HTTPGetMethod;

/***
 * 
 * @author Mourad TOUMI
 *
 */
public class LoggerListener {
	
	private String fLogUrl;
	
	private List<String> fLog;
	
	private boolean fJustStarted = true;
	
	public LoggerListener(final String pLogUrl, final long pInterval) {
		fLogUrl = pLogUrl;
		fLog = new ArrayList<String>();
	}
	
	public List<BOAbstractEvent> getGameEvents() throws IOException{
		
		final List<String> tmpLog = HTTPGetMethod.get(fLogUrl);
		
		final List<BOAbstractEvent> eventResult = new ArrayList<BOAbstractEvent>();
		
		//Traitement des evenement du log
		
		for (String bLine : tmpLog) {
			final String iLine = bLine.replaceFirst("^\\s+", "");
			
			if (!fLog.contains(iLine)) {
				BOAbstractEvent event = null;
				
				fLog.add(iLine);
				// Le traiter ici meme -> Extraire l'AbstractEventAction :)
				if (iLine.split(";").length > 1) { // Event Joueur

					final String type = iLine.split("\\s+")[1].split(";")[0];
					//"\\[[0-Z]*\\]";
					final String playerId = iLine.split("\\s+")[1].split(";")[1];

					if (type.equals("K")) {
						final String attack = iLine.split("\\s+")[1].split(";")[9];
						event = new KillEvent(attack);
					} else if (type.equals("Weapon")) {
						event = new WeaponEvent();	
					} else if (type.equals("D")) {
						event = new DeadEvent();
					} else if (type.equals("say")) {
						event = new SayEvent();
						((SayEvent)event).setMessage(iLine.split(";")[4]);
						((SayEvent)event).getMessage();
					} else if (type.equals("Q")) {
						event = new QuitEvent();
					} else if (type.equals("J")) {
						event = new JoinEvent();	
					}
					
					if (event != null) {
						event.setPlayerId(playerId);
						event.setLog(iLine);
						
						eventResult.add(event);
					}
				} else if (iLine.split(":").length > 1) { // Event Jeu
					
					if (iLine.contains("InitGame")) {
						event = new InitGameEvent();
						
					} else if (iLine.contains("ShutdownGame")) {
						event = new ShutdownGame();
					} else if (iLine.contains("ExitLevel")) {
						event = new ExitLevelEvent();
					}
					
					if (event != null) {
						event.setLog(iLine);
						eventResult.add(event);
					}
					
				} else { // �a restera un mystere :D
					
				}
			}
		}
		
		if (!fJustStarted) {
			return eventResult;
		} else {
			fJustStarted = false;
			return new ArrayList<BOAbstractEvent>();
		}
		
			   	
	}
	
	public void init() {
		
		
		
	}

	private void clearLog() {
		fLog.clear();
	}

}
